package com.thenie.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thenie.chat.mapper.AddRequestMapper;
import com.thenie.chat.pojo.AddRequest;
import com.thenie.chat.service.IAddRequestService;
import com.thenie.chat.vo.FriendRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thenie
 * @since 2022-06-03
 */
@Service
public class AddRequestServiceImpl extends ServiceImpl<AddRequestMapper, AddRequest> implements IAddRequestService {

    @Autowired
    private AddRequestMapper addRequestMapper;

    /**
     * 插入一条添加请求
     * @param fromId
     * @param toId
     * @return
     */
    public int insertRequest(long fromId,long toId,int sign){
        AddRequest addRequest = new AddRequest();
        addRequest.setFromId(fromId);
        addRequest.setToId(toId);
        addRequest.setSign(sign);

        return addRequestMapper.insert(addRequest);
    }
    /**
     * 好友请求记录
     * @param fromId
     * @param toId
     * @return
     */
    public AddRequest queryAddRequestFromMe(long fromId,long toId){
        AddRequest addRequest = addRequestMapper.selectOne(new QueryWrapper<AddRequest>()
                .eq("from_id", fromId)
                .eq("to_id", toId)
        );
        return addRequest;
    }



    /**
     * 修改状态
     * @param addRequest
     * @return
     */
    public int updateSign(AddRequest addRequest){
        int over = addRequestMapper.update(addRequest, new UpdateWrapper<AddRequest>()
                .eq("from_id", addRequest.getFromId())
                .eq("to_id", addRequest.getToId()));
        return over;
    }

    /**
     * 查询好友请求记录列表
     * @param id
     * @return
     */
    public List<FriendRequestVo> queryFriendRequestList(Long id){
        return addRequestMapper.queryFriendRequestList(id);
    }
}
